package com.pro.controller;

import com.pro.pojo.JsUser;
import com.pro.service.JsRoleAuthorityService;
import com.pro.service.JsUserService;
import com.pro.utils.ExcelUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLOutput;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author MYH - SiYun
 * @date 2022/1/14 9:06
 * 管理员导入
 */

@RequestMapping("/ExcelJsUser")
@Controller
public class ExcelJsUserController {

    @Autowired
    private JsUserService jsUserService;
    @Autowired
    private JsRoleAuthorityService jsRoleAuthorityService;


    @RequestMapping("/importJsUser")
    @ResponseBody
    public int importExcel(MultipartFile fil) throws IOException, ParseException {
        String end = fil.getOriginalFilename().substring(fil.getOriginalFilename().indexOf("."));
        List<JsUser> pro = null;
        if(end.equals(".xls")){
            pro = xlsExcel(fil);
        }else{
            pro = xlsxExcel(fil);
        }

        List<String> jsUserAccount = jsUserService.getJsUserAccount();
        for (int i = 0; i < pro.size(); i++) {
            for (int j = 0; j < jsUserAccount.size(); j++) {
                if(pro.get(i).getAccount().equals(jsUserAccount.get(j))){
                    System.out.println(123);
                    return 2;
                }
            }
        }
        if(pro != null){
            boolean importJsUser = jsUserService.importJsUser(pro);
            for (JsUser jsUser : pro) {
                jsUser.setDeptId(1);
                jsUser.setType(2);
                jsUser.setAuthorityId(1);
            }
            boolean addUsersAuthoritys = jsRoleAuthorityService.addUsersAuthoritys(pro);
            boolean addUsersDept = jsUserService.addUsersDept(pro);
            if(importJsUser && addUsersAuthoritys && addUsersDept){
                return 200;
            }
            return 0;
        }
        return 0;
    }


    private List<JsUser> xlsExcel(MultipartFile fil) throws IOException, ParseException {
        ArrayList<JsUser> list = new ArrayList<>();
        InputStream inputStream = fil.getInputStream();
        HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
        HSSFSheet sheet= workbook.getSheetAt(0);

        for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
            HSSFRow row = sheet.getRow(rowNum);
            JsUser jsUser = null;
            if(row != null){
                jsUser = new JsUser();
                jsUser.setTrueName(row.getCell(1).toString());
                jsUser.setSex(row.getCell(2).toString());
                jsUser.setAccount(row.getCell(3).toString());
                jsUser.setAcatarUrl(row.getCell(4).toString());
                jsUser.setEmail(row.getCell(5).toString());
                jsUser.setPassword(row.getCell(6).toString());
                jsUser.setRemark(row.getCell(7).toString());
                jsUser.setEnabled(ExcelUtils.StrisInt(row.getCell(8).toString()));
                jsUser.setPhone(row.getCell(9).toString());
                jsUser.setIdCard(row.getCell(10).toString());
                jsUser.setBirthDate(row.getCell(11).toString());
                jsUser.setFreestyle(row.getCell(12).toString());
                jsUser.setRegisterDate(row.getCell(13).toString());
                jsUser.setRegisterIp(row.getCell(14).toString());
                jsUser.setStatus(ExcelUtils.StrisInt(row.getCell(15).toString()));
                jsUser.setGrade(ExcelUtils.StrisInt(row.getCell(16).toString()));
                jsUser.setWages(ExcelUtils.StrisInt(row.getCell(17).toString()));
            }
            list.add(jsUser);
        }
        return list;
    }

    private List<JsUser> xlsxExcel(MultipartFile fil) throws IOException, ParseException {
        ArrayList<JsUser> list = new ArrayList<>();
        InputStream inputStream = fil.getInputStream();
        XSSFWorkbook Workbook = new XSSFWorkbook(inputStream);
        XSSFSheet sheet = Workbook.getSheetAt(0);

        for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
            XSSFRow row = sheet.getRow(rowNum);
            JsUser jsUser = null;
            if(row != null){
                jsUser = new JsUser();
                jsUser.setTrueName(row.getCell(1).toString());
                jsUser.setSex(row.getCell(2).toString());
                jsUser.setAccount(row.getCell(3).toString());
                jsUser.setAcatarUrl(row.getCell(4).toString());
                jsUser.setEmail(row.getCell(5).toString());
                jsUser.setPassword(row.getCell(6).toString());
                jsUser.setRemark(row.getCell(7).toString());
                jsUser.setEnabled(ExcelUtils.StrisInt(row.getCell(8).toString()));
                jsUser.setPhone(row.getCell(9).toString());
                jsUser.setIdCard(row.getCell(10).toString());
                jsUser.setBirthDate(row.getCell(11).toString());
                jsUser.setFreestyle(row.getCell(12).toString());
                jsUser.setRegisterDate(row.getCell(13).toString());
                jsUser.setRegisterIp(row.getCell(14).toString());
                jsUser.setStatus(ExcelUtils.StrisInt(row.getCell(15).toString()));
                jsUser.setGrade(ExcelUtils.StrisInt(row.getCell(16).toString()));
                jsUser.setWages(ExcelUtils.StrisInt(row.getCell(17).toString()));
            }
            list.add(jsUser);
        }
        return list;
    }

}
